# ===8种玩法=======
# A: 创建 - [], list(), range()
# (1)
from collections.abc import Iterable
names = ['zhangsan', 'lisi', 'wangwu']
empty_names = []
# (2)
names = list()  # 创建1个空的列表
# 可以接受一个iterable的对象
names = list(range(8))
names = list((1, 2, 3, 4))
names = list('hello')
# 检测对象是否为iterable对象
# print(isinstance('hello', Iterable))

# B: 增(append, insert, extend, +)
names = []
# (1) append
names.append('hello')
names.append(3)
names.append((2, 4, 6))
# (2) insert
names.insert(0, 1)
names.insert(-2, 3)
# (3) extend
name1 = ['D', 'J']
names.extend(name1)
# (4) +
name1 = ['D', 'J']
names += name1

# C: 删除元素的3种方法：del, pop. remove
# (1) del
# print(names)
del names[1]
# print(names)
# (2) pop
names.pop()
names.pop(1)
# print(names)
# (3) remove
# - 只会删除第一个符合条件的
# - 如果没有符合条件的，就抛出ValueError
names.remove('D')
# print(names)

# D: 修改元素的值
names[1] = '改'
# print(names)

# E：查 查询元素[], index(), len()
# (1): 下标从0开始，也可以从-1开始
# (2): 下标越界，会出现IndexError
names[0]
names[-1]
# (3): index查询下标，如果没有，会抛出ValueError
names.index('D')
# (4): in, not in
if 'D' not in names:
    names.append('D')
else:
    names.index('D')
# (5): len()
len(names)

# F: 操作 loop, +, *, ==, sort, reverse, max, min
# (1): loop
# for index, n in enumerate(names):
#     print(f'no {index + 1} is {n}')
# (2): 基本运算
scores = [1, 2, 3, 4]
scores2 = [1, 2, 3, 5]
# print(names + scores)
# print(scores * 2)
# print(scores == scores2)
# (3): 排序 ordered sorted
scores.reverse()
scores.sort()
scores.sort(reverse=True)
sorted(scores, reverse=True)

name_scores = [('zhangsan', 87), ('lisi', 78), ('DJ', 100)]
# print(sorted(name_scores, key=lambda n: n[1]))
# (4): 切片操作 start:end:step
# print(names)
# print(names[-1:1:-2])

# G: 复 推导式Comprehension, map, filter
names = [1, 2, 3, 4, 5]
names2 = names[:]

nums = list(range(100))
sq_nums = [n*n for n in nums]
nums_3 = [n for n in nums if n % 3 == 0]
# print(sq_nums)
# print(nums_3)
